<?php
/**
 * Created by PhpStorm.
 * User: sundy
 * Date: 2020/2/3
 * Time: 10:11
 */

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use App\WxUser;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class WxUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '会员中心';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WxUser);
        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->like('openid', 'OpenId');
            $filter->like('nickname', 'User');
        });
        $grid->quickSearch('openid','nickname');

        $grid->number('Num');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });
        $grid->column('openid', __('OpenId'));
        $grid->column('', __('User'))->unescape()->display(function (){
            return '<div style="width: 120px;height: 30px;overflow: hidden;"><div style="float: left;width: 70px;">'.$this->nickname.'</div><img src="'.$this->headimgurl.'" style="width: 30px;height: 30px;padding-left:5px;float: right"></div>';
        });
        $grid->column('sex', __('Sex'))->unescape()->display(function (){
            if($this->sex==1){
                return '男';
            }
            if($this->sex==2){
                return '女';
            }
            if($this->sex==0){
                return '未知';
            }
        });
        $grid->column('rank', __('Rank'))->unescape()->display(function (){
            if($this->rank==0){
                return '普通会员';
            }
            if($this->rank==1){
                return '高级会员';
            }
            if($this->rank==2){
                return '代理商';
            }
            if($this->rank==3){
                return '普通管理员';
            }
            if($this->rank==4){
                return '超级管理员';
            }
        });
        $grid->column('city', __('City'));
        $grid->column('province', __('Province'));
        $grid->column('country', __('Country'));


        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
        });
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        return $grid;
    }

    protected function detail($openid)
    {
        if(!is_numeric($openid)){
            $id =  DB::table('wx_users')->where('openid',$openid)->value('i_id');
        } else {
            $id = $openid;
        }
        $show = new Show(WxUser::findOrFail($id));
        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
        });
        $show->field('openid', __('OpenId'));
        $show->field('', __('User'))->unescape()->as(function (){
            return '<div style="width: 100px;height: 30px;overflow: hidden;"><div style="float: left">'.$this->nickname.'</div><img src="'.$this->headimgurl.'" style="width: 30px;height: 30px;padding-left:5px;float: right"></div>';
        });

        $show->field('sex', __('Sex'))->unescape()->as(function (){
            if($this->sex==1){
                return '男';
            }
            if($this->sex==2){
                return '女';
            }
            if($this->sex==0){
                return '未知';
            }
        });
        $show->field('city', __('City'));
        $show->field('province', __('Rrovince'));

        $show->field('language', __('Language'));
        $show->field('subscribe_time', __('Subscribe Time'));
        $show->field('remark', __('Remark'));
        $show->field('groupid', __('GroupId'));
        $show->field('tagid_list', __('Tagid List'));
        $show->field('subscribe_scene', __('Subscribe Scene'));
        $show->field('qr_scene', __('QR Scene'));
        $show->field('qr_scene_str', __('QR Scene Str'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WxUser);
        $form->select('rank','Rank')->options([0=>'普通会员',1=>'高级会员',2=>'代理商',3=>'普通管理员',4=>'超级管理员']);
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();

        });
        return $form;
    }
}
